package com.beyond.trace.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.domain.TraceSale;
import com.beyond.trace.service.ITraceSaleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 销售信息Controller
 * 
 * @author yao
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/trace/sale")
@Api(value = "销售信息", tags = "销售信息")
public class TraceSaleController extends BaseController
{
    @Autowired
    private ITraceSaleService traceSaleService;

    //@PreAuthorize("@ss.hasPermi('beyond:sale:list')")
    @GetMapping("/list")
    @ApiOperation("查询销售信息列表")
    public TableDataInfo list(TraceSale traceSale)
    {
        startPage();
        List<TraceSale> list = traceSaleService.selectTraceSaleList(traceSale);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('beyond:sale:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询销售信息列表不分页")
    public AjaxResult getInfoQuery(TraceSale traceSale)
    {
        return AjaxResult.success(traceSaleService.selectTraceSaleList(traceSale));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:sale:export')")
    @Log(title = "销售信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceSale traceSale)
    {
        List<TraceSale> list = traceSaleService.selectTraceSaleList(traceSale);
        ExcelUtil<TraceSale> util = new ExcelUtil<TraceSale>(TraceSale.class);
        return util.exportExcel(list, "销售信息数据");
    }

    //@PreAuthorize("@ss.hasPermi('beyond:sale:query')")
    @GetMapping(value = "/{saleId}")
    @ApiOperation("查询销售信息列表")
    public AjaxResult getInfo(@PathVariable("saleId") Long saleId)
    {
        return AjaxResult.success(traceSaleService.selectTraceSaleById(saleId));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:sale:add')")
    @Log(title = "销售信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增销售信息")
    public AjaxResult add(@RequestBody TraceSale traceSale)
    {
        if(StringUtils.isBlank(traceSale.getSaleProduct())){
            return AjaxResult.error("请选择产品");
        }else{
            return toAjax(traceSaleService.insertTraceSale(traceSale));
        }
    }

    //@PreAuthorize("@ss.hasPermi('beyond:sale:edit')")
    @Log(title = "销售信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改销售信息")
    public AjaxResult edit(@RequestBody TraceSale traceSale)
    {
        return toAjax(traceSaleService.updateTraceSale(traceSale));
    }

    //@PreAuthorize("@ss.hasPermi('beyond:sale:remove')")
    @Log(title = "销售信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{saleIds}")
    @ApiOperation("删除销售信息")
    public AjaxResult remove(@PathVariable Long[] saleIds)
    {
        return toAjax(traceSaleService.deleteTraceSaleByIds(saleIds));
    }

    @Log(title = "门店图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("门店图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/sale", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

}
